package com.wyzz.hr.report.action;

import java.util.*;
import javax.servlet.ServletRequest;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.pojo.Empsalarydatadef;
import com.wyzz.hr.report.bo.SalaryReportBo;
import com.wyzz.hr.spring.SpringBeanFactory;

public class SalaryRport extends BaseAction {

	public SalaryRport() {
		reportFormat = "HTML";
		itemId = "";
		expression = "";
		reportFileLocation = "";
	}

	public String execute() throws Exception {
		if (StringUtils.isEmpty(reportFileLocation)
				|| StringUtils.isEmpty(itemId)
				|| StringUtils.isEmpty(expression))
			return "input";
		SalaryReportBo salaryReportBo = (SalaryReportBo) SpringBeanFactory
				.getBean("salaryReportBo");
		Empsalarydatadef dataDef = (Empsalarydatadef) salaryReportBo
				.loadObject(Empsalarydatadef.class, itemId, null);
		if (dataDef == null) {
			addErrorInfo("\u4E0D\u5B58\u5728\u7684\u5E10\u5957\u9879\u76EE\uFF01");
			return "input";
		}
		String title = dataDef.getEsddName();
		title = title != null ? title : "";
		title = (new StringBuilder()).append(getCurrentYearAndMonth()).append(
				title).toString();
		java.util.List reportItems = salaryReportBo.getObjects(itemId,
				expression);
		ServletRequest request = getRequest();
		if ("PDF".equalsIgnoreCase(reportFormat)) {
			request.setAttribute("_format", "PDF");
			request.setAttribute("_contentType", "attachment");
			request.setAttribute("_fileName", "empReport");
		} else {
			request.setAttribute("_format", "HTML");
		}
		Map params = new HashMap();
		params.put("itemId", itemId);
		params.put("reportObjects", reportItems);
		params.put("title", title);
		request.setAttribute("_params", params);
		request.setAttribute("_report", reportFileLocation);
		return "success";
	}

	private String getCurrentYearAndMonth() {
		Calendar c = Calendar.getInstance();
		String result = (new StringBuilder()).append(String.valueOf(c.get(1)))
				.append("\u5E74").toString();
		result = (new StringBuilder()).append(result).append(
				String.valueOf(c.get(2) + 1)).append("\u6708").toString();
		return result;
	}

	public String getReportFormat() {
		return reportFormat;
	}

	public void setReportFormat(String reportFormat) {
		this.reportFormat = reportFormat;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public String getExpression() {
		return expression;
	}

	public void setExpression(String expression) {
		this.expression = expression;
	}

	public String getReportFileLocation() {
		return reportFileLocation;
	}

	public void setReportFileLocation(String reportFileLocation) {
		this.reportFileLocation = reportFileLocation;
	}

	private String reportFormat;
	private String itemId;
	private String expression;
	private String reportFileLocation;
}
